package com.ruoyi.system.service.customer.impl;

import com.ruoyi.common.constant.UserConstants;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.system.domain.supe.DistributionTeam;
import com.ruoyi.system.mapper.zhuangxiumapper.DistributionTeamMapper;
import com.ruoyi.system.service.customer.DistributionTeamService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;
import java.util.Random;

@Service
public class DistributionTeamServiceImpl implements DistributionTeamService {

    @Autowired
    private DistributionTeamMapper distributionTeamMapper;

    /**
     * 查询商家分销
     *
     * @param distributionId 商家分销ID
     * @return 商家分销
     */
    @Override
    public DistributionTeam selectDistributionTeamById(Long distributionId)
    {
        return distributionTeamMapper.selectDistributionTeamById(distributionId);
    }

    @Override
    public List<DistributionTeam> supplierIdById(Long supplierId) {
        return distributionTeamMapper.supplierIdById(supplierId);
    }

    @Override
    public String checksupplierIdUnique(Long supplierId) {
        int count = distributionTeamMapper.checksupplierIdUnique(supplierId);
        if (count > 0) {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    @Override
    public String checkPhoneUnique(DistributionTeam distributionTeam) {
        Long userId = StringUtils.isNull(distributionTeam.getDistributionId()) ? -1L : distributionTeam.getDistributionId();
        DistributionTeam info = distributionTeamMapper.checkPhoneUnique(distributionTeam.getSupplierId());
        if (StringUtils.isNotNull(info) && info.getDistributionId().longValue() != userId.longValue())
        {
            return UserConstants.NOT_UNIQUE;
        }
        return UserConstants.UNIQUE;
    }

    /**
     * 查询商家分销列表
     *
     * @param distributionTeam 商家分销
     * @return 商家分销
     */
    @Override
    public List<DistributionTeam> selectDistributionTeamList(DistributionTeam distributionTeam)
    {
        return distributionTeamMapper.selectDistributionTeamList(distributionTeam);
    }

    /**
     * 新增商家分销
     *
     * @param distributionTeam 商家分销
     * @return 结果
     */
    @Override
    public int insertDistributionTeam(DistributionTeam distributionTeam)
    {

        Long supplierIdfx = distributionTeam.getSupplierIdfx();
        distributionTeam.setSupplierId(supplierIdfx);

        StringBuilder stringBuilder = new StringBuilder();
        Random random = new Random();
        for (int i=0;i<8;i++){
            stringBuilder.append(random.nextInt(10));
        }
        String num =stringBuilder.toString();
        distributionTeam.setDistributionCode(num);
        distributionTeam.setModifiedTime(new Date());
        distributionTeam.setDistributionState("1");
        return distributionTeamMapper.insertDistributionTeam(distributionTeam);
    }

    /**
     * 修改商家分销
     *
     * @param distributionTeam 商家分销
     * @return 结果
     */
    @Override
    public int updateDistributionTeam(DistributionTeam distributionTeam)
    {
        return distributionTeamMapper.updateDistributionTeam(distributionTeam);
    }

    /**
     * 批量删除商家分销
     *
     * @param distributionIds 需要删除的商家分销ID
     * @return 结果
     */
    @Override
    public int deleteDistributionTeamByIds(Long[] distributionIds)
    {
        return distributionTeamMapper.deleteDistributionTeamByIds(distributionIds);
    }

    /**
     * 删除商家分销信息
     *
     * @param distributionId 商家分销ID
     * @return 结果
     */
    @Override
    public int deleteDistributionTeamById(Long distributionId)
    {
        return distributionTeamMapper.deleteDistributionTeamById(distributionId);
    }

}
